<?php

/***************************************
 *	短信发送
 *	2012-09-24
 ***************************************/

 class SMS{

	private $post_data = array();
	private $smsapi;

//$api='http://api.pipc.net/api.aspx';
//$post_data = array('Action'=>"SendSMS", 'UserName'=>"qq22715352", 'UserKey'=>"AFE432EC-A1A1-478A-83EF-80AF23953B61", 'Mobiles'=>"13910733547", 'Content'=>"短信测试，带有签名的 " . date("y-m-d h:i:s",time()));

//$post_data = array("CorpID"=>"jasonhere", "Pwd"=>"123456", "Mobile"=>"15201476609", "Content"=>"这个是测试，不用回复")
//$api = "http://web.iscall.com/ws/Send.aspx";


	public function __construct($mobile, $content, $url=null, $_post_data=array(), $is_utf8 = false){
		if($url==null){
			$this->smsapi = __get_config('SMS.url');
		}else{
			$this->smsapi = $url;
		}
		
		if(!is_array($_post_data) || empty($_post_data)){
			$this->post_data = __get_config('SMS.setup');
		}else{
			$this->post_data = $_post_data;
		}
		
		$this->post_data["Mobile"]=$mobile;
		$this->post_data["Content"]=$content;
		
		if(!$is_utf8 && "UTF-8" == mb_detect_encoding($this->post_data["Content"])){//检测字符串编码  
			$this->post_data["Content"]=mb_convert_encoding($this->post_data["Content"],"GB2312","UTF-8");
		}
	}
	
	//public function 

	public function send(){
		
		$o="";
		foreach ($this->post_data as $k=>$v)
		{
		    $o.= "$k=".urlencode($v)."&";
		}

		//echo $o . "<p>";
		 
		$post_data=substr($o,0,-1);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_URL,$this->smsapi);
		//为了支持cookie
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$back = curl_exec($ch);

		$result = explode("|", $back);

		$log = " [". date("Y-m-d H:i:s") ."]\t";
		$log .= $this->post_data["Mobile"]."\t";
		$log .= $this->post_data["Content"]."\t";
		$log .= $back. " \n";

		$_log_path = MC_ROOT_DIR.DS.__get_config('Log.Log_dir').DS.'sms.log';
		//用于使用非mcphp框架内的文件测试使用
		if(!is_file($_log_path)) $_log_path=MC_ROOT_DIR.DS.'log/do_test_error.log';
		//if(__get_config('Common.HostEnv') != 'prod'){
			error_log($log, 3, $_log_path);
		//}

		if($result[0] == 0){
			return true;
		}else{
			return false;
		}
	}

	function sendAPI2(){
		$api='http://api.pipc.net/api.aspx';
		$post_data = array('Action'=>"SendSMS", 'UserName'=>"qq22715352", 'UserKey'=>"AFE432EC-A1A1-478A-83EF-80AF23953B61", 'Mobiles'=>$this->post_data["Mobile"], 'Content'=>$this->post_data["Content"]);

		$o="";
		foreach ($post_data as $k=>$v)
		{
		    $o.= "$k=".urlencode($v)."&";
		}

		//echo $o . "<p>";
		 
		$post_data=substr($o,0,-1);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_URL,$api);
		//为了支持cookie
		curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$back = curl_exec($ch);

		$result = explode("|", $back);

		$log = " [". date("Y-m-d H:i:s") ."]\t";
		$log .= $this->post_data["Mobile"]."\t";
		$log .= $this->post_data["Content"]."\t";
		$log .= $back. " \n";

		if(__get_config('Common.HostEnv') != 'prod'){
			if( "UTF-8" == mb_detect_encoding($log)){//检测字符串编码  
				$log=mb_convert_encoding($log,"GB2312","UTF-8");
			}
			$_log_path = MC_ROOT_DIR.DS.__get_config('Log.Log_dir').DS.'MCPHP_Error.log';
			//用于使用非mcphp框架内的文件测试使用
			if(!is_file($_log_path)) $_log_path=MC_ROOT_DIR.DS.'log/do_test_error.log';
			if(__get_config('Common.HostEnv') != 'prod'){
				error_log($log, 3, $_log_path);
			}
		}

		return $result;


	}

 }

 ?>
